package controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import  domain.*;
import javafx.stage.Stage;
import service.CustomerService;
import  service.impl.*;
import utill.ParamUtil;
import utill.switchpages;

import java.io.IOException;
import java.util.List;

public class CustomerSelectController {

    @FXML
    private TextField GoutputIdText;

    @FXML
    private Menu titleSignout;

    @FXML
    private Label GoutputLabelone;

    @FXML
    private TableColumn<?, ?> flowerType;

    @FXML
    private TableView<flower> orderTableView;

    @FXML
    private TableColumn<?, ?> flowerId;

    @FXML
    private TableColumn<?, ?> flowerPirce;

    @FXML
    private MenuItem btnsignout;

    @FXML
    private TableColumn<?, ?> flowerTotal;

    @FXML
    private Menu cheeckflower;

    @FXML
    private Button btncommit;

    @FXML
    private Menu btncallback;

    @FXML
    private TextField GoutputTotal;

    @FXML
    private Label GoutputLabelthree;

    @FXML
    private TextField Goutputpirce;

    @FXML
    private Label GoutputLabeltwo;

    @FXML
    private GridPane GoutputFactory;

    @FXML
    private MenuItem Titlechecked;

    @FXML
    private Button btnClerar;


    @FXML
    private AnchorPane ParentPane;



    CustomerService customerService =new CustomerServiceImpl();
    String url="/controller/login.fxml";
    String BuyUrl="/controller/CustomerOrder.fxml";


    @FXML
    void JumpBuyFlower(ActionEvent event) throws Exception {
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.BuyUrl);
    }

    @FXML
    void btncallback(ActionEvent event) throws Exception {
        ParamUtil.userId.remove(Thread.currentThread().getName()+"userId");
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.url);
    }

    @FXML
    void signout(ActionEvent event) throws Exception {
        ParamUtil.userId.remove(Thread.currentThread().getName()+"userId");
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.url);
    }

    @FXML
    void btnselectflower(ActionEvent event) {
        //删除表中所有列（数据）
        orderTableView.getItems().clear();
        List<flower> flowerList = customerService.findAllFlower(false);
        bindTableView(orderTableView,flowerId,flowerType,flowerPirce,flowerTotal);
        ObservableList<flower> data = FXCollections.observableArrayList();

        for(flower f : flowerList){
            data.add(f);
        }
        orderTableView.setItems(data);
    }

    @FXML
    void clear(ActionEvent event) {
        TextClear();
    }

    @FXML
    void commit(ActionEvent event) {
         orderTableView.getItems().clear();

         String flowerid=GoutputIdText.getText();
         String flowertotal=GoutputTotal.getText();
         String flowerprice=Goutputpirce.getText();
         List<flower> flowerList =customerService.findSomeFlower(flowerid,flowertotal,flowerprice);
         int count=0;

         bindTableView(orderTableView,flowerId,flowerType,flowerPirce,flowerTotal);
         ObservableList<flower> data = FXCollections.observableArrayList();

         for(flower f : flowerList){
             System.out.println(f);
             if(count>5) break;
         }

         orderTableView.setItems(data);
         TextClear();

    }

    /**
     * @param tableView
     * @Param column
     */
    public void bindTableView(TableView  tableView ,  TableColumn column1 ,
                              TableColumn column2 ,TableColumn column3,TableColumn column4){
        column1.setCellValueFactory(new PropertyValueFactory<>("flowerId"));
        column2.setCellValueFactory(new PropertyValueFactory<>("flowerType"));
        column3.setCellValueFactory(new PropertyValueFactory<>("flowerPrice"));
        column4.setCellValueFactory(new PropertyValueFactory<>("flowertotal"));
    }


    public  void TextClear(){
        Goutputpirce.setText("");
        GoutputIdText.setText("");
        GoutputTotal.setText("");
    }

}
